package model.assessment;

import model.Database;
import model.UID_Generator;
import net.passClass.PassData;
import net.passClass.PassTemplate;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Objects;


public class TemplateDB implements Database<Template>{
    private ArrayList<Template> templates;

    public ArrayList<Template> getTemplates() {
        return templates;
    }
    private static TemplateDB instance = null;
    private TemplateDB() {}
    private TemplateDB(ArrayList<Template> templates) {
        this.templates = Objects.requireNonNullElseGet(templates, ArrayList::new);
    }
    public static TemplateDB getInstance() {
        if (instance == null){
            instance = new TemplateDB();
        }
        return instance;
    }
    public static void initialize(ArrayList<Template> templates) {
        if (instance == null){
            instance = new TemplateDB(templates);
        }
    }

    @Override
    public PassData add(Template template) {
        for(Template aTemplate: templates){
            if(!aTemplate.isDeleted() &&
                    aTemplate.getName().equals(template.getName())){
                return new PassData(false, null,"问题重复，添加失败");
            }
        }
        template.setUid(UID_Generator.getUid("T"));
        templates.add(template);
        return new PassData(true, null,"添加成功");
    }

    @Override
    public PassData update(Template template){
        if(!templates.contains(template)){
            return new PassData(false, null,"模板不存在，修改失败");
        }
        Template theTemplate = templates.get(templates.indexOf(template));
        theTemplate.setDeleted(true);
        templates.add(template);
        return new PassData(true, null,"修改成功");
    }

    @Override
    public PassData delete(Template template) {
        if(!templates.contains(template)){
            return new PassData(false, null,"模板不存在，修改失败");
        }
        Template theTemplate = templates.get(templates.indexOf(template));
        theTemplate.setDeleted(true);
        return new PassData(true, null,"删除成功");
    }

    @Override
    public PassTemplate get(Template template){
        LinkedList<Template> tmpAns = new LinkedList<>(templates);
        tmpAns.removeIf(Template::isDeleted);

        if(template.getUid()!=null) {
            if (tmpAns.contains(template)) {
                ArrayList<Template> ans = new ArrayList<>();
                ans.add(tmpAns.get(tmpAns.indexOf(template)));
                return new PassTemplate(true, ans, "查找成功");
            } else {
                return new PassTemplate(true, null, "不存在查找结果");
            }
        }

        if(template.getName()!=null){
            tmpAns.removeIf(aTemplate -> !aTemplate.getName()
                    .contains(template.getName()));
        }

        if(template.getQuestionUID()!=null){
            for(String uid: template.getQuestionUID()){
                tmpAns.removeIf(aTemplate -> !aTemplate.getQuestionUID()
                        .contains(uid));
            }
        }

        if(template.getType()!=null){
            tmpAns.removeIf(aTemplate -> !aTemplate.getType()
                    .contains(template.getType()));
        }

        ArrayList<Template> ans = new ArrayList<>(tmpAns);
        return new PassTemplate(true, ans,"查找成功");
    }

    public PassData addQuestion(Template template, String questionUID){
        if(!templates.contains(template)){
            return new PassData(false, null,"模板不存在，问题新增失败");
        }
        Template theTemplate = templates.get(templates.indexOf(template));
        if(theTemplate.getQuestionUID().contains(questionUID)){
            return new PassData(false, null,"问题已存在,问题新增失败");
        }
        theTemplate.getQuestionUID().add(questionUID);
        return new PassData(true, null,"问题新增成功");
    }

    public PassData deleteQuestion(Template template, String questionUID){
        if(!templates.contains(template)){
            return new PassData(false, null,"模板不存在，问题删除失败");
        }
        Template theTemplate = templates.get(templates.indexOf(template));
        if(!theTemplate.getQuestionUID().contains(questionUID)){
            return new PassData(false, null,"问题不存在,问题删除失败");
        }
        theTemplate.getQuestionUID().remove(questionUID);
        return new PassData(true, null,"问题删除成功");
    }

}
